package ifrn.scartissue.client;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

import marauroa.client.BannedAddressException;
import marauroa.client.TimeoutException;
import marauroa.common.net.InvalidVersionException;

public class GameWindow extends JFrame {

	JButton logoutBtn;
	public GameWindow() {
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setMinimumSize(new Dimension(300, 300));
		initComponents();
		this.setVisible(true);
	}
	
	private void initComponents(){
		logoutBtn = new JButton("Logout");
		logoutBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				tryLogout();
			}
		});
		GridLayout layout = new GridLayout(3,4);
		setLayout(layout);
		
		add(logoutBtn);
		pack();
	}
	
	private void tryLogout(){
		MarauroaClient client = MarauroaClient.get();
		try {
			client.logout();
			alert("Logout efetuado com sucesso!");
			Client.doLogout();
		} catch (InvalidVersionException e) {
			// TODO Auto-generated catch block
			alertError("Versão Inválida");
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			alertError("Tempo Excedido");
			e.printStackTrace();
		} catch (BannedAddressException e) {
			// TODO Auto-generated catch block
			alertError("Endereço banido");
			e.printStackTrace();
		}	
		
	}
	
	private void alert(String message) {
		JOptionPane.showMessageDialog(this, message);
	}
	
	private void alertError(String message) {
		JOptionPane.showMessageDialog(this, message, "Erro", JOptionPane.ERROR_MESSAGE);
	}
}
